package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttg.common.exception.BaseException;
import com.ttg.model.pojo.BankOrderCreditInfoPrimitive;
import com.ttg.model.mapper.BankOrderCreditInfoPrimitiveMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.web.dao.BankOrderCreditInfoPrimitiveDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (TblOrderCreditInfo) daoImpl
 *
 * @author chenyou
 * @datetime 2025-11-12 13:53:43
 */
@Service
public class BankOrderCreditInfoPrimitiveDaoImpl extends ServiceImpl<BankOrderCreditInfoPrimitiveMapper, BankOrderCreditInfoPrimitive> implements BankOrderCreditInfoPrimitiveDao {

    @Override
    public boolean deleteByOrdNos(List<String> ordNos) {
        return baseMapper.deleteByOrdNos(ordNos);
    }

    @Override
    public boolean deleteByDay(LocalDate day) {
        LocalDateTime start = day.atStartOfDay();
        LocalDateTime end   = day.atTime(23, 59, 59);
        LambdaQueryWrapper<BankOrderCreditInfoPrimitive> w = new LambdaQueryWrapper<>();
        w.between(BankOrderCreditInfoPrimitive::getTradeTime, start, end);
        return baseMapper.delete(w)>0;
    }

    @Override
    public void efficientBatchSaveOrUpdate(List<BankOrderCreditInfoPrimitive> list){
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        if(list.size()>1000){
            throw new BaseException("批次更新数量不能超过1000");
        }
        // 1. 查询已存在的记录
        List<BankOrderCreditInfoPrimitive> existRecords = baseMapper.selectExistByMultiFields(list);
        Map<String, BankOrderCreditInfoPrimitive> existRecordMap = existRecords.stream()
                .collect(Collectors.toMap(
                        record -> buildUniqueKey(record.getOrdNo()),
                        Function.identity()
                ));

        // 2. 分离需要插入和更新的数据
        List<BankOrderCreditInfoPrimitive> toInsert = new ArrayList<>();
        List<BankOrderCreditInfoPrimitive> toUpdate = new ArrayList<>();

        for (BankOrderCreditInfoPrimitive record : list) {
            String uniqueKey = buildUniqueKey(record.getOrdNo());
            BankOrderCreditInfoPrimitive existRecord = existRecordMap.get(uniqueKey);

            if (existRecord != null) {
                // 设置ID用于更新
                record.setId(existRecord.getId());
                toUpdate.add(record);
            } else {
                toInsert.add(record);
            }
        }

        // 3. 批量操作
        if (!toInsert.isEmpty()) {
            this.saveBatch(toInsert);
        }
        if (!toUpdate.isEmpty()) {
            this.updateBatchById(toUpdate);
        }
    }


    private String buildUniqueKey(String ordNo) {
        return ordNo;
    }
}

